package com.example.springboot.utils;

import com.example.springboot.entity.Post;
import com.example.springboot.entity.vo.PostVo;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CommonUtil {
    public static Map<String, Object> postToMap(Post post) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = post.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(post);
            map.put(fieldName, value);
        }
        return map;
    }
    public static Map<String, Object> postVoToMap(PostVo postVo) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = postVo.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(postVo);
            map.put(fieldName, value);
        }
        return map;
    }
    public static Post mapToPost(Map<Object, Object> map) throws IllegalAccessException {
        Post post = new Post();
        Class<?> clazz = post.getClass();
        for (Map.Entry<Object, Object> entry : map.entrySet()) {
            String fieldName = entry.getKey().toString(); // 将键转换为字符串类型
            Object value = entry.getValue();
            Field field;
            try {
                field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                // 根据字段的类型进行类型转换并设置值
                setValueForField(post, field, value);
            } catch (NoSuchFieldException e) {
                // 如果字段在 Post 类中不存在，可以选择忽略或者记录日志
                System.out.println("Field '" + fieldName + "' does not exist in Post class.");
            }
        }
        return post;
    }

    // 设置字段值的方法，根据字段的类型进行适当的类型转换
    private static void setValueForField(Post post, Field field, Object value) throws IllegalAccessException {
        if (value == null) {
            // 可以根据字段类型设置默认值或者特定处理
            if (field.getType() == String.class) {
                field.set(post, ""); // 设置空字符串
            } else if (field.getType() == Long.class || field.getType() == long.class) {
                field.set(post, 0L); // 设置默认的长整型值
            } else if (field.getType() == Integer.class || field.getType() == int.class) {
                field.set(post, 0); // 设置默认的整型值
            }
            // 可以根据需要添加其他类型的处理逻辑
        } else {
            // 非空值情况下的处理逻辑
            Class<?> fieldType = field.getType();
            if (fieldType == Long.class || fieldType == long.class) {
                field.set(post, Long.parseLong(value.toString()));
            } else if (fieldType == String.class) {
                field.set(post, value.toString());
            } else if (fieldType == Integer.class || fieldType == int.class) {
                field.set(post, Integer.parseInt(value.toString()));
            } else if (fieldType == Timestamp.class) {
                try {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
                    Date parsedDate = dateFormat.parse(value.toString());
                    Timestamp timestamp = new Timestamp(parsedDate.getTime());
                    field.set(post, timestamp);
                } catch (ParseException e) {
                    System.out.println("Error parsing date for field '" + field.getName() + "'");
                }
            }
            // 可以继续添加其他类型的处理逻辑
        }
    }

}
